package com.example.yamba;

/**
 * Created by Jagery on 2015-8-18.
 */
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import com.sina.weibo.sdk.openapi.models.Status;

import android.util.Log;
public class StatusData  {
    private static final String DB_NAME = "timeline.db";
    private static final int VERSION = 2;
    private static final String TAG = "StatusData" ;
    private Context context;
    static final String TABLE = "timeline";
    static final String C_ID = BaseColumns._ID;
    static final String C_CREATE_AT = "create_at";
    static final String C_STATUS = "status";
    static final String C_USER = "user";
    class DBHelper extends SQLiteOpenHelper{
    	public DBHelper(Context context){
    		super(context,DB_NAME,null,VERSION);
    	}
    	@Override
        public void onCreate(SQLiteDatabase db) {
            String sql = "create table "+TABLE+
                    "( "+C_ID+" INTEGER PRIMARY KEY,"+
                        C_CREATE_AT +" TEXT,"+
                        C_USER+" TEXT,"+
                        C_STATUS+" TEXT)";
            db.execSQL(sql);
            Log.i(TAG,"create table= "+sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop table if exists "+TABLE);
            Log.i(TAG,"Upgrade DB");
            onCreate(db);
        }
    }
    private final DBHelper dbhelper ;
    public StatusData (Context context){
        this.context = context; 
        dbhelper = new DBHelper(context);
    }
    public void close(){
    	this.dbhelper.close();
    	Log.i(TAG,"DBHelper is closed");
    }
    public long insertOrIgnore(ContentValues value){
    	//Log.i(TAG,"insertOrIgnore on "+ value);
    	SQLiteDatabase db = this.dbhelper.getWritableDatabase();
    	try{
    		return db.insertWithOnConflict(TABLE,null,value,SQLiteDatabase.CONFLICT_IGNORE);
    	}finally{
    		db.close();
    	}
    }
    public Cursor getStatusUpdates(){
    	SQLiteDatabase db = this.dbhelper.getReadableDatabase();
    	return db.query(TABLE,null,null,null,null,null,"create_at DESC");
    }
    
    //@return Timestamp of the lastest status we have it the database
    public long getLastestStatusCreateAtTime(){
    	SQLiteDatabase db  = this.dbhelper.getWritableDatabase();
    	try{
    		Cursor cursor = db.query(TABLE,new String[]{"create_at"},null,null,null,null,null);
    		try{
				long time = 0;
    			while(cursor.moveToNext()){
    				String str = cursor.getString(0);    				
    				long temp  = Long.parseLong(str);
    				if(temp>time){
						time = temp;
					}
    			}
				Log.i(TAG,"LastestTime is "+ time);
				return time;
    		} finally{
    			cursor.close();
        	}
    	}finally{
    		db.close();
    	}
    }
    public Status getStatusTextById(long id){
    	SQLiteDatabase db = this.dbhelper.getReadableDatabase();
    	try{
    		Cursor cursor = db.query(TABLE,new String[]{C_STATUS},"id="+id,null,null,null,null);
    		try{
    			String text = cursor.moveToNext()?cursor.getString(0):null;
    			return text==null?null:Status.parse(text);
    		}finally{
    			cursor.close();
    		}
    	}finally{
    		db.close();
    	}
    }
	public DBHelper getDbhelper(){
		return dbhelper;
	}
}
